package com.example.demo.dao;

import com.example.demo.entity.Sys_role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;


@Repository
public class Sys_roleDaoImpl implements Sys_roleDao{
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int saveSys_role(Sys_role sys_role) {
        String sql = "insert into sys_role(name,description,create_time,create_by,update_time," +
                "update_by,del_flag)"+ "values(?,?,?,?,?,?,?)" ;
        int result = jdbcTemplate.update(sql, new Object[]{sys_role.getName(),
                sys_role.getDescription(), sys_role.getCreate_time(), sys_role.getCreate_by(),
                sys_role.getUpdate_time(), sys_role.getUpdate_time(), sys_role.getDel_flag()});

        return result;
    }



    @Override
    public List<Sys_role> getAllSys_role() {
        String sql = "select * from sys_role";
        List<Sys_role> sys_roles = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Sys_role.class));
        return sys_roles;

    }

    @Override
    public Sys_role getSys_roleById(Integer id) {
        String sql = "select * from sys_role where id=?";
        Sys_role sys_role = jdbcTemplate.queryForObject(sql, new Object[]{id},
                new BeanPropertyRowMapper<>(Sys_role.class));
        return sys_role;
    }

    @Override
    public int updateSys_role(Integer id) {
        String sql = "update sys_role set name='zhangsan' where id=?";
        int result = jdbcTemplate.update(sql, id);
        return result;
    }

    @Override
    public int deleteSys_roleById(Integer id) {
        String sql = "delete from sys_role where id=?";
        int result = jdbcTemplate.update(sql, id);
        return result;
    }
}
